<template>
	<div class="system-dept-container">
		<!-- 查询条件-->
		<el-card shadow="hover">
			<el-form inline v-model="state.params" size="small">
				<el-form-item label="参数名称">
					<el-input v-model="state.params.paramName" placeholder="搜索参数名称"></el-input>
				</el-form-item>
				<el-form-item label="参数Key">
					<el-input v-model="state.params.paramKey" placeholder="搜索参数Key"></el-input>
				</el-form-item>
				<el-form-item label="平台参数">
					<el-select v-model="state.params.platform" placeholder="是否平台参数" clearable>
						<el-option label="是" value="true"></el-option>
						<el-option label="否" value="false"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="是否必选">
					<el-select v-model="state.params.mandatory" placeholder="是否必选" clearable>
						<el-option label="是" value="true"></el-option>
						<el-option label="否" value="false"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" class="ml10" @click="initTableData">
						<el-icon>
							<elementSearch />
						</el-icon>
						查询
					</el-button>
				</el-form-item>
			</el-form>
		</el-card>
		<!--分割线-->
		<el-divider></el-divider>
		<!--数据展示和分页-->
		<el-card shadow="never">
			<template #header>
				<div class="card-header">
					<span>系统参数表列表</span>
					<!--新增等操作区域-->
					<el-button size="small" type="success" class="ml10" @click="openAddWindow">
						<el-icon>
							<elementDocumentAdd />
						</el-icon>
						新增系统参数表
					</el-button>
				</div>
			</template>
			<!--表格展示区-->
			<el-table :data="state.tableData.data" style="width: 100%">
				<el-table-column align="center" label="参数名称" prop="paramName" show-overflow-tooltip />
				<el-table-column align="center" label="参数的key" prop="paramKey" show-overflow-tooltip />
				<el-table-column align="center" label="参数的value" prop="paramValue" show-overflow-tooltip />
				<el-table-column align="center" label="参数类型" prop="paramValue" show-overflow-tooltip>
					<template #default="scope">
						<el-tag type="success">{{ scope.row.valueType == 1 ? '文本' : scope.row.valueType == 2 ? '图片' : '链接' }}</el-tag>
					</template>
				</el-table-column>
				<el-table-column align="center" label="是否必选" prop="mandatory" show-overflow-tooltip>
					<template #default="scope">
						<el-tag :type="scope.row.mandatory ? 'success' : 'info'">{{ scope.row.mandatory ? '是' : '否' }}</el-tag>
					</template>
				</el-table-column>
				<el-table-column align="center" label="是否平台" prop="platform" show-overflow-tooltip>
					<template #default="scope">
						<el-tag :type="scope.row.platform ? 'success' : 'info'">{{ scope.row.platform ? '是' : '否' }}</el-tag>
					</template>
				</el-table-column>
				<el-table-column align="center" label="是否只读" prop="readonly" show-overflow-tooltip>
					<template #default="scope">
						<el-tag :type="scope.row.readonly ? 'success' : 'info'">{{ scope.row.readonly ? '是' : '否' }}</el-tag>
					</template>
				</el-table-column>
				<el-table-column label="操作" show-overflow-tooltip width="140">
					<template #default="scope">
						<el-button size="mini" type="text" @click="openEditWindow(scope.row)">修改</el-button>
						<el-popconfirm
							confirm-button-text="删除"
							cancel-button-text="取消"
							icon-color="red"
							title="确定删除这条记录吗？"
							@confirm="delRow(scope.row)"
						>
							<template #reference>
								<el-button size="mini" type="text">删除</el-button>
							</template>
						</el-popconfirm>
					</template>
				</el-table-column>
			</el-table>
			<!--分页-->
			<el-pagination
				@size-change="onHandleSizeChange"
				@current-change="onHandleCurrentChange"
				class="mt15"
				:pager-count="5"
				:page-sizes="[10, 20, 30]"
				v-model:current-page="state.params.pageNo"
				background
				v-model:page-size="state.params.pageSize"
				layout="total, sizes, prev, pager, next, jumper"
				:total="state.tableData.total"
			>
			</el-pagination>
		</el-card>

		<AddSysParams ref="addRef" @initTableData="initTableData" />
		<EditSysParams ref="editRef" @initTableData="initTableData" />
	</div>
</template>

<script lang="ts" setup>
import { onMounted, reactive, ref } from 'vue';
import { getSysParamsList, delSysParams } from '/@/api/sys/param';
import AddSysParams from '/@/views/system/param/component/addSysParams.vue';
import EditSysParams from '/@/views/system/param/component/editSysParams.vue';

const addRef = ref();
const editRef = ref();

const state = reactive({
	tableData: {
		data: [],
		total: 0,
		loading: false,
	},
	params: {
		pageNo: 1,
		pageSize: 10,
		paramName: '',
		paramKey: '',
		mandatory: '',
		platform: '',
	},
});
// 初始化表格数据
const initTableData = () => {
	getSysParamsList(state.params).then((res: any) => {
		state.tableData.data = res.data;
		state.tableData.total = res.extra.total;
	});
};

//分页大小改变
const onHandleSizeChange = (val: number) => {
	state.params.pageSize = val;
	initTableData();
};
// 分页改变
const onHandleCurrentChange = (val: number) => {
	state.params.pageNo = val;
	initTableData();
};

// 打开新增菜单弹窗
const openAddWindow = () => {
	addRef.value.openWindows();
};

const openEditWindow = (row: any) => {
	editRef.value.openWindows(row.paramId);
};

const delRow = (row: any) => {
	delSysParams(row.paramId).then(() => {
		initTableData();
	});
};

// 页面加载时
onMounted(() => {
	initTableData();
});
</script>

<style scoped></style>
